/* $RCSFile: ADNDynProductPriceLevel.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynProductPriceLevel.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynProductPricelevel : DynEntity
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.productpricelevel)
            {}

            public override Type DynType
            { get { return typeof(ADNDynProductPricelevel); } }

            public override string DynTypename
            {   get{ return CrmStr.productpricelevel;}}

            public override DynEntity Build()
            {   return new ADNDynProductPricelevel();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynProductPricelevel(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynProductPricelevel(w);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynProductPricelevel(w,PrimaryKeyValue);}
        }

        static ADNDynProductPricelevel(){new Ftor();} 

        public override bool HasPrimaryField
        {
            get{   return false;}
        }

        public override bool HasOwnerProperty
        {
            get{   return false;}
        }

        public override bool HasOwninguser
        {
            get{   return false;}
        }

        public override bool HasBusinessUnitProperty
        {
            get{   return false;}
        }

        public override bool HasOwningBusinessUnitProperty
        {
            get{   return false;}
        }

        public override bool OwningbusinessunitAsUniqueIdentifier
        {
            get{   return false;}
        }

        /// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynProductPricelevel(): base()
		{}

		public ADNDynProductPricelevel(CRMWrapperBase w)
			: base(w,CrmStr.productpricelevel)
		{}

		public ADNDynProductPricelevel(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.productpricelevel, CrmId)
		{}

		public ADNDynProductPricelevel(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.productpricelevel,e,null,null)
		{}

		public ADNDynProductPricelevel(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(w,CrmStr.productpricelevel,e,prop,s)
		{}

		public ADNDynProductPricelevel(ResetWrapperHandler fn)
			: base(fn,CrmStr.productpricelevel)
		{}

		public ADNDynProductPricelevel(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.productpricelevel, CrmId)
		{}

		public ADNDynProductPricelevel(ResetWrapperHandler fn, InternalDynamicEntity e)
			: base(fn,CrmStr.productpricelevel,e,null,null)
		{}

		public ADNDynProductPricelevel(ResetWrapperHandler fn, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(fn,CrmStr.productpricelevel,e,prop,s)
		{}


		/// <summary>
		/// Crm Key
		/// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public Guid productpricelevelid
		{
			get
			{
				return KeyPropertyValue(CrmStr.productpricelevelid);
			}
			set
			{
				AddKeyProperty(CrmStr.productpricelevelid,value);
			}
		}

        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.productpricelevelid) || KeyPropertyValue(CrmStr.productpricelevelid) != crmId )
                productpricelevelid =   crmId;
        }
 
        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return productpricelevelid;
           }
           set
           {
               productpricelevelid    =   value;
           }
        }

        /// <summary>
        /// Gets the exchange rate for the currency associated with the product price level with respect to the base currency. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public decimal exchangerate
        {
            get
            {
                return DecimalPropertyValue(CrmStr.exchangerate);
            }
            set
            {
                AddDecimalProperty(CrmStr.exchangerate,value);
            }
        }

        /// <summary>
        /// Gets and sets the ID of the currency associated with the product price list item. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public Guid transactioncurrencyid
        {
            get
            {
                return LookupPropertyValue(CrmStr.transactioncurrencyid);
            }
            set
            {
                AddLookupProperty(CrmStr.transactioncurrencyid,CrmStr.transactioncurrency,value);
            }
        }

        public string transactioncurrencyName
        {
            get
            {
                return LookupPropertyName(CrmStr.transactioncurrencyid);
            }
        }

        /// <summary>
        /// Gets the ID of the discount list associated with the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public Guid discounttypeid
        {
            get 
            {
                if ( ContainsKey(CrmStr.discounttypeid) )
                    return LookupPropertyValue(CrmStr.discounttypeid);
                return Guid.Empty;
            }
            set
            {
                AddLookupProperty(CrmStr.discounttypeid,CrmStr.discounttype,value);
            }
        }
 
        public string discounttypeName
        {
            get 
            {
                if ( ContainsKey(CrmStr.discounttypeid) )
                    return LookupPropertyName(CrmStr.discounttypeid);
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets or sets the product associated with the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public Guid productid
        {
            get 
            {
                return LookupPropertyValue(CrmStr.productid);
            }
            set
            {
                AddLookupProperty(CrmStr.productid,CrmStr.product,value);
            }
        }
 
        public string productName
        {
            get 
            {
                if ( ContainsKey(CrmStr.productid) )
                    return LookupPropertyName(CrmStr.productid);
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets or sets the ID of the unit for the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public Guid uomscheduleid
        {
            get
            {
                if ( ContainsKey(CrmStr.uomscheduleid) )
                    return LookupPropertyValue(CrmStr.uomscheduleid);
                return Guid.Empty;
            }
            set
            {
                AddLookupProperty(CrmStr.uomscheduleid,CrmStr.uomschedule,value);
            }
        }

        public string uomscheduleName
        {
            get
            {
                if ( ContainsKey(CrmStr.uomscheduleid) )
                    return LookupPropertyName(CrmStr.uomscheduleid);
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets or sets the ID of the unit for the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public Guid uomid 
        {
            get
            {
                if ( ContainsKey(CrmStr.uomid) )
                    return LookupPropertyValue(CrmStr.uomid);
                return Guid.Empty;
            }
            set
            {
                AddLookupProperty(CrmStr.uomid,CrmStr.uom,value);
            }
        }

        public string uomName
        {
            get
            {
                if ( ContainsKey(CrmStr.uomid) )
                    return LookupPropertyName(CrmStr.uomid);
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets or sets the rounding option amount for the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public decimal roundingoptionamount
        {
            get
            {
                if ( dicProperty.ContainsKey(CrmStr.roundingoptionamount) )
                    return MoneyPropertyValue(CrmStr.roundingoptionamount);
                return 0;
            }
            set
            {
                AddMoneyProperty(CrmStr.roundingoptionamount,value);
            }
        }

        /// <summary>
        /// Gets the base currency equivalent of the rounding option amount for the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public decimal roundingoptionamount_base
        {
            get
            {
                if ( dicProperty.ContainsKey(CrmStr.roundingoptionamount_base) )
                    return MoneyPropertyValue(CrmStr.roundingoptionamount_base);
                return 0;
            }
        }

        /// <summary>
        /// Gets or sets the percentage of the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public decimal percentage
        {
            get
            {
                if ( dicProperty.ContainsKey(CrmStr.percentage) )
                    return DecimalPropertyValue(CrmStr.percentage);
                return 0;
            }
            set
            {
                AddDecimalProperty(CrmStr.percentage,value);
            }
        }

        /// <summary>
        /// Gets or sets the policy for rounding the price list. 
        /// None
        /// Up
        /// Down
        /// To Nearest        
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public int roundingpolicycode
        {
            get
            {
                return PicklistPropertyIntValue(CrmStr.roundingpolicycode);
            }
            set
            {
                AddPicklistProperty(CrmStr.roundingpolicycode,value);
            }
        }
         
        public string roundingpolicycodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.roundingpolicycode);
            }
        }

        /// <summary>
        /// Gets or sets the option for rounding the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public int roundingoptioncode
        {
            get
            {
                return PicklistPropertyIntValue(CrmStr.roundingoptioncode);
            }
            set
            {
                AddPicklistProperty(CrmStr.roundingoptioncode,value);
            }
        }
         
        public string roundingoptioncodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.roundingoptioncode);
            }
        }

        /// <summary>
        /// Gets or sets the quantity of the product that must be sold for a given price level
        /// No Control
        /// Whole
        /// Whole and Fractional
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public int quantitysellingcode
        {
            get
            {
                return PicklistPropertyIntValue(CrmStr.quantitysellingcode);
            }
            set
            {
                AddPicklistProperty(CrmStr.quantitysellingcode,value);
            }
        }
         
        public string quantitysellingcodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.quantitysellingcode);
            }
        }

        /// <summary>
        /// Gets or sets the pricing method applied to the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public int pricingmethodcode
        {
            get
            {
                return PicklistPropertyIntValue(CrmStr.pricingmethodcode);
            }
            set
            {
                AddPicklistProperty(CrmStr.pricingmethodcode,value);
            }
        }
         
        public string pricingmethodcodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.pricingmethodcode);
            }
        }

        /// <summary>
        /// Gets or sets the monetary amount for the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public decimal amount
        {
            get
            {
                if ( ContainsKey(CrmStr.amount) )
                    return MoneyPropertyValue(CrmStr.amount);
                return 0m;
            }
            set
            {
                AddMoneyProperty(CrmStr.amount,value);
            }
        }

        /// <summary>
        /// Gets the base currency equivalent of the monetary amount for the price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public decimal amount_base
        {
            get
            {
                if ( ContainsKey(CrmStr.amount_base) )
                    return MoneyPropertyValue(CrmStr.amount_base);
                return 0m;
            }
        }

        /// <summary>
        ///  Gets or sets the ID of the price level associated with this price list. 
        /// </summary>
        [CrmDynAttribute(CrmStr.productpricelevel)]
        public Guid pricelevelid
        {
            get 
            {
                return LookupPropertyValue(CrmStr.pricelevelid);
            }
            set
            {
                AddLookupProperty(CrmStr.pricelevelid,CrmStr.pricelevel,value);
            }
        }

        public string pricelevelName
        {
            get 
            {
                if ( ContainsKey(CrmStr.pricelevelid) )
                    return LookupPropertyName(CrmStr.pricelevelid);
                return string.Empty;
            }
        }

        /// <summary>
        /// find  by selecting same pricelevel same product 
        /// Copy the found properties in current object
        /// </summary>
        public bool FindByPricelevelProduct(ResetWrapperHandler fn,Guid plID,Guid prodID,Guid UnitID,bool isAdmin)
        {
            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DynEntity FindByPricelevelProduct BEG plID: {0}, prodID: {1}, UnitID: {2}, isAdmin: {3}.",
                    new object[]{plID,prodID,UnitID,isAdmin}),CRMDynTraceSwitch.Sw.Info);
            try
            {
                GenericDynQueryMultiple<ADNDynProductPricelevel> allEntQuery      =   new GenericDynQueryMultiple<ADNDynProductPricelevel>(wrapper,fn,CrmStr.productpricelevel);
                
                InternalQueryExpression qe                          =   new InternalQueryExpression();
                qe.EntityName                                       =   CrmStr.productpricelevel;
                InternalAllColumns RequestCols                      =   new InternalAllColumns();
                InternalFilterExpression fi                         =   new InternalFilterExpression();
                InternalConditionExpression ex                      =   new InternalConditionExpression();
                ex.AttributeName                                    =   CrmStr.pricelevelid;
                ex.Operator                                         =   InternalConditionOperator.Equal;
                ex.Values                                           =   new object[]{plID};
                
                InternalConditionExpression ex1                     =   new InternalConditionExpression();
                ex1.AttributeName                                   =   CrmStr.productid;
                ex1.Operator                                        =   InternalConditionOperator.Equal;
                ex1.Values                                          =   new object[]{prodID};

                InternalConditionExpression ex2                     =   new InternalConditionExpression();
                ex2.AttributeName                                   =   CrmStr.uomid;
                ex2.Operator                                        =   InternalConditionOperator.Equal;
                ex2.Values                                          =   new object[]{UnitID};
                
                fi.Conditions                                       =   new InternalConditionExpression[]{ex,ex1,ex2};
                qe.ColumnSet                                        =   RequestCols;
                qe.Criteria                                         =   fi;
                
                InternalBusinessEntityCollection allEnt             =   (isAdmin) ? allEntQuery.adminRetrieve(qe): allEntQuery.Retrieve(qe);
                ADNDynProductPricelevel ob                          =   null;
                bool found                                          =   false;
                if ( allEntQuery.RetrievedCount > 0 )
                {
                    wrapper.Util.ExecForEachDynamicEntity(allEnt,delegate(InternalDynamicEntity de)
                        {
                            ob                                      =   new ADNDynProductPricelevel(wrapper,de);
                            wrapper.FillDicProperties(de,null,dicProperty);
                            found                                   =   true;
                            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                                Trace.WriteLine( "[V]ADNDynProductPricelevel FindByPricelevelProduct found.",CRMDynTraceSwitch.Sw.Info);
                            return false; //break
                        });
                }
                return found;
            }
            catch(Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynProductPricelevel FindByPricelevelProduct plID: {0}, prodID: {1}, ex: {2}",
                        plID,prodID,ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
        }
        
        /// <summary>
        /// find  by selecting same pricelevel same product 
        /// </summary>
        static public ADNDynProductPricelevel FindByPricelevelProduct(CRMWrapperBase wrapper,ResetWrapperHandler fn,Guid plID,Guid prodID,Guid UnitID,bool isAdmin)
        {
            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DynEntity FindByPricelevelProduct BEG plID: {0}, prodID: {1}, UnitID: {2}, isAdmin: {3}.",
                    new object[]{plID,prodID,UnitID,isAdmin}),CRMDynTraceSwitch.Sw.Info);
            try
            {
                GenericDynQueryMultiple<ADNDynProductPricelevel> allEntQuery      =   new GenericDynQueryMultiple<ADNDynProductPricelevel>(wrapper,fn,CrmStr.productpricelevel);
                
                InternalQueryExpression qe                          =   new InternalQueryExpression();
                qe.EntityName                                       =   CrmStr.productpricelevel;
                InternalAllColumns RequestCols                      =   new InternalAllColumns();
                InternalFilterExpression fi                         =   new InternalFilterExpression();
                
                InternalConditionExpression ex                      =   new InternalConditionExpression();
                ex.AttributeName                                    =   CrmStr.pricelevelid;
                ex.Operator                                         =   InternalConditionOperator.Equal;
                ex.Values                                           =   new object[]{plID};
                
                InternalConditionExpression ex1                     =   new InternalConditionExpression();
                ex1.AttributeName                                   =   CrmStr.productid;
                ex1.Operator                                        =   InternalConditionOperator.Equal;
                ex1.Values                                          =   new object[]{prodID};

                InternalConditionExpression ex2                     =   new InternalConditionExpression();
                ex2.AttributeName                                   =   CrmStr.uomid;
                ex2.Operator                                        =   InternalConditionOperator.Equal;
                ex2.Values                                          =   new object[]{UnitID};
                
                fi.Conditions                                       =   new InternalConditionExpression[]{ex,ex1,ex2};
                qe.ColumnSet                                        =   RequestCols;
                qe.Criteria                                         =   fi;
                
                InternalBusinessEntityCollection allEnt             =   (isAdmin) ? allEntQuery.adminRetrieve(qe): allEntQuery.Retrieve(qe);
                ADNDynProductPricelevel ob                          =   null;
                if ( allEntQuery.RetrievedCount > 0 )
                {
                    wrapper.Util.ExecForEachDynamicEntity(allEnt,delegate(InternalDynamicEntity de)
                        {
                            if ( fn != null )
                                 ob                                 =   new ADNDynProductPricelevel(fn,de);
                            else ob                                 =   new ADNDynProductPricelevel(wrapper,de);
                            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                                Trace.WriteLine( "[V]ADNDynProductPricelevel FindByPricelevelProduct found.",CRMDynTraceSwitch.Sw.Info);
                            return false; //break
                        });
                }
                return ob;
            }
            catch(Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynProductPricelevel FindByPricelevelProduct plID: {0}, prodID: {1}, ex: {2}",
                        plID,prodID,ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
        }
    }
}
